package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HZF
 * @date 2020/7/1 20:51
 * @description
 */
public class ReadXMLUtil {

    private List<Map<String, Host>> serverList;

    public ReadXMLUtil(List<Map<String, Host>> serverList) {
        this.serverList = serverList;
    }

    public void loadServlet() throws DocumentException {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(resourceAsStream);
        Element rootElement = document.getRootElement();
        List<Element> selectNodes = rootElement.selectNodes("//Server");
        for (int i = 0; i < selectNodes.size(); i++) {
            Element element = selectNodes.get(i);
            List<Element> serviceNodes = element.selectNodes("//Service");
            //遍历
            for (Element service:serviceNodes){
                Element connectorNode = (Element) service.selectSingleNode("//Connector");
                String port = connectorNode.attributeValue("port");
                Element engineNode = (Element) service.selectSingleNode("//Engine");
                List<Element> hostNodes = engineNode.selectNodes("//Host");
                
                //service和engine同级处理，遍历host
                Map<String,Context> contextMap = new HashMap<String,Context>();
                for (Element hostNo:hostNodes){
                    //获取每个host的名称和项目路径
                    String hostName = hostNo.attributeValue("name");
                    String appBase = hostNo.attributeValue("appBase");
                    File file = new File(appBase);
                    if(!file.exists() || file.list()==null){
                        break;
                    }
                    String[] list = file.list();
                    //遍历子文件夹，既实际的项目列表
                    for (String path:list){
                        //将项目封装成context，并保存入map
                        contextMap.put(path,loadContextServlet(appBase+"\\"+path+"\\"));
                    }
                }
                
            }
        }
    }

    private Context loadContextServlet(String path) {
        String webPath = path+"web.xml";
        if(!(new File(webPath).exists())){
            System.out.println("未找到"+path+"web.xml");
            return null;
        }
        try {
            FileInputStream resourceAsStream = new FileInputStream(webPath);
            SAXReader saxReader = new SAXReader();

            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();
            List<Element> selectNodes = rootElement.selectNodes("//servlet");
            Map<String,Wrapper> wrapperMap = new HashMap<String,Wrapper>();
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element = selectNodes.get(i);
                Element servletNameElement = (Element) element.selectSingleNode("servlet-name");
                String servletName = servletNameElement.getStringValue();
                //<servlet-class>server.LagouServlet</servlet-class>
                Element servletClassElement = (Element) element.selectSingleNode("servlet-class");
                String servletClass = servletClassElement.getStringValue();
                //加载这个class文件
                MyClassLoader myClassLoader = new MyClassLoader(path);
                Class<?> servletClass1 = myClassLoader.loadClass(servletClass);
                //根据servlet-name的值找到url-pattern
                Element servletMapping =(Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                // /lagou
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
                HttpServlet servlet = (HttpServlet) servletClass1.newInstance();
                //将最后获得的servlet存入wrapper,并且存入wrapperMap
                wrapperMap.put(urlPattern,new Wrapper(servlet));
            }
            return  new Context(wrapperMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
